import {defineStore} from "pinia";
import UserApi from "../api/user";
export const useUserStore = defineStore("user", {
    state: ()=>{
        return{
            isLogin: false,
            userData: {}
        };
    },
    getters: {},
    actions: {
    loginState(userData) {
      this.isLogin = true;
      this.userData = userData;
    },
    logoutState() {
      this.isLogin = false;
      this.userData = {};
    },
    async isAuth() {
      if (this.userData) {
        const api = new UserApi();
        let res = await api.find(this.userData.userId);
        if (res.code === 401 || res.code === 403) {
          this.isLogin = false;
          this.userData = {};
        }
      } else {
        this.isLogin = false;
        this.userData = {};
      }
    }
  },
    persist: true
});